package org.litespring.core.io;

import org.litespring.util.Assert;
import org.litespring.util.ClassUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author ChangLiang
 * @date 2020/7/22
 */
public class FileSystemResource implements Resource{

    private final String path;

    private ClassLoader classLoader;

    private final File file;

    public FileSystemResource(File file) {
        this.path = file.getPath();
        this.file = file;
    }

    public FileSystemResource(String path) {
        Assert.notNull(path, "path must not be null");
        this.file = new File(path);
        this.path = path;
    }

    public FileSystemResource(String path, ClassLoader classLoader) {
        this(path);
        this.classLoader = classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader();
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new FileInputStream(this.file);
    }

    @Override
    public String getDescription() {
        return this.path;
    }
}
